﻿using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Input;
using ScrumFactory.Windows.Helpers;
using ScrumFactory.Composition.View;

namespace ScrumFactory.Windows
{

    [Export]
    public partial class MainWindow : Window, IDialogView
    {
        private object model;

        private const int DOCK_PANEL_WIDTH = 340;

        public MainWindow()
        {
            this.InitializeComponent();

            leftPanels.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(leftPanels_SelectionChanged);

        
            dockWindowContainer.MouseEnter += new MouseEventHandler(leftPanelsBorder_MouseLeave);

        
            
            StateChanged += new System.EventHandler(MainWindow_StateChanged);

            Closed += new System.EventHandler(MainWindow_Closed);
        }

        
        void MainWindow_Closed(object sender, System.EventArgs e) {
            Application.Current.Shutdown();
        }

        public new void Show() {
            WindowState = System.Windows.WindowState.Normal;
            SetMaximized();
            base.Show();
        }

        void MainWindow_StateChanged(object sender, System.EventArgs e) {
            if (WindowState == System.Windows.WindowState.Minimized) {
                ShowInTaskbar = true;                
            }
            else {
                SetMaximized();
                ShowInTaskbar = true;
            }
        }

        public void CloseAllWindows() {
            dockWindowContainer.CloseAllWindows();
        }

        private void SetMaximized() {            
            Left = 0;
            Top = 0;
            Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            Height = System.Windows.SystemParameters.WorkArea.Height;
           
        }

        

        void leftPanelsBorder_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) {
            if (fixDock.IsChecked==true)
                return;
            leftPanels.SelectedItem = null;                                 
        }

        [Import(typeof(ViewModel.ShellViewModel))]
        public object Model
        {
            get
            {
                return this.model;
            }
            set
            {
                this.model = value;
                this.DataContext = value;                
            }
        }

        private void leftPanels_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {
            if (!leftPanels.IsEnabled) {                
                leftColumn.Width = new GridLength(100, GridUnitType.Auto);
                leftColumn.MaxWidth = DOCK_PANEL_WIDTH;
                leftGridSplitter.IsEnabled = false;                
                return;
            }
            if (leftPanels.SelectedItem == null) {
                leftColumn.MinWidth = 0;
                leftColumn.Width = new GridLength(100, GridUnitType.Auto);
                leftGridSplitter.IsEnabled = false;
                fixDock.IsChecked = false;
                fixDock.Visibility = Visibility.Collapsed;
            }
            else {
                int width = DOCK_PANEL_WIDTH;                
                leftGridSplitter.IsEnabled = true;
                leftColumn.MinWidth = width;
                leftColumn.MaxWidth = double.PositiveInfinity;
                leftColumn.Width = new GridLength(width, GridUnitType.Pixel);
                fixDock.Visibility = Visibility.Visible;
            }
        }

        
              



        

    }
}
